/**
 * Formulario para Alteraracao da Fonte
 *
 * @this {UIFontDialog}
 * @version
 */

function UIFontDialog() {

    /**
     * Variaveis
     */
    var _oFontDialog = null;
    var _oFontToolBar = null;
    var _oClassOwner = this;
    
    /**
     * Inicializa o objeto da classe
     */
    this.init = function() {

        // Dialog Fonte
        this.initFontDialog();

        // ToolBar Fonte
        this.initFontToolBar();
    }

    /**
     * Funcoes
     */
    this.initFontDialog = function() {
        
        _oFontDialog = new YAHOO.widget.Dialog("fontdialog", {
            constraintoviewport : true,
            fixedcenter : true,
            modal: false
        });
        var handleSubmit = function() {
            //edita as propriedades de fonte do elemento selecionado, referentes aos valores escolhidos no toolbar
            _oClassOwner.setFontPropertiesOfElementSelected();
            _oFontDialog.hide();
        };
        var myButtons = [ {
            text:"OK",
            name:"fontbutton",
            handler:handleSubmit,
            isDefault:true
        } ];
        _oFontDialog.cfg.queueProperty("buttons", myButtons);
    }

    this.initFontToolBar = function() {

        _oFontToolBar = new YAHOO.widget.Toolbar("fonttoolbar", {
            buttonType: 'advanced',
            buttons: [
            {
                group: 'textstyle',
                label: 'Fonte',
                buttons: [
                {
                    type: 'push',
                    label: 'Negrito',
                    value: 'bold',
                    id: 'bold'
                },

                {
                    type: 'push',
                    label: 'Italico',
                    value: 'italic',
                    id: 'italic'
                },

                {
                    type: 'push',
                    label: 'Sublinhado',
                    value: 'underline',
                    id: 'underline'
                },

                {
                    type: 'separator'
                },

                {
                    type: 'select',
                    label: 'Arial',
                    value: 'fontname',
                    id: 'fontname',
                    menu: [
                    {
                        text: 'Arial',
                        checked: true
                    },

                    {
                        text: 'Arial Black'
                    },

                    {
                        text: 'Comic Sans MS'
                    },

                    {
                        text: 'Courier New'
                    },

                    {
                        text: 'Lucida Console'
                    },

                    {
                        text: 'Serif'
                    },

                    {
                        text: 'Tahoma'
                    },

                    {
                        text: 'Times New Roman'
                    },

                    {
                        text: 'Trebuchet MS'
                    },

                    {
                        text: 'Verdana'
                    }
                    ]
                },
                {
                    type: 'spin',
                    label: '13',
                    value: 'fontsize',
                    id: 'fontsize',
                    range: [ 9, 75 ]
                },

                {
                    type: 'separator'
                },

                {
                    type: 'color',
                    label: 'Cor da Fonte',
                    value: 'forecolor',
                    id: 'forecolor'
                },

                {
                    type: 'color',
                    label: 'Cor de Fundo',
                    value: 'backcolor',
                    id: 'backcolor'
                }
                ]
            },

            {
                group: 'linestyle',
                label: 'Estilo da Linha',
                buttons: [
                {
                    type: 'select',
                    label: '',
                    value: 'dashtype',
                    id: 'dashtype',
                    menu: [
                    {
                        text: EGeneralTypes.eStrokeDashSolidType,
                        checked: true
                    },

                    {
                        text: EGeneralTypes.eStrokeDashTracedType
                    }
                    ]
                }
                ]
            },

            {
                group: 'linestrokewidth',
                label: 'Espessura',
                buttons: [
                {
                    type: 'spin',
                    label: '5',
                    value: 'strokewidth',
                    id: 'strokewidth',
                    range: [ 1, 20 ]
                }
                ]
            }
            ]
        });

        _oFontToolBar.on("buttonClick", function(info) {
            
            var button = _oFontToolBar.getButtonByValue(info.button.value);
            _oFontToolBar.deselectAllButtons();
            _oFontToolBar.selectButton(button);

            switch(info.button.id) {

                case "bold":
                    //alert(info.button.value);
                    _oFontToolBar.getButtonByIndex("0").set("value", info.button.value);
                    break;
                case "italic":
                    //alert(info.button.value);
                    _oFontToolBar.getButtonByIndex("1").set("value", info.button.value);
                    break;
                case "underline":
                    //alert(info.button.value);
                    _oFontToolBar.getButtonByIndex("2").set("value", info.button.value);
                    break;
                case "fontname":
                    //alert(info.button.value);
                    _oFontToolBar.getButtonByIndex("3").set("value", info.button.value);
                    break;
                case "fontsize":
                    //alert(button.get("label"));
                    _oFontToolBar.getButtonByIndex("4").set("value", button.get("label"));
                    break;
                case "dashtype":
                    //alert(info.button.value);
                    _oFontToolBar.getButtonByIndex("7").set("value", info.button.value);
                    break;
                case "strokewidth":
                    //alert(button.get("label"));
                    _oFontToolBar.getButtonByIndex("8").set("value", button.get("label"));
                    break;
                default:
                    if(info.button.value == "forecolor") {
                        //alert(info.button.color);
                        _oFontToolBar.getButtonByIndex("5").set("value", "#"+info.button.color);
                    }
                    if(info.button.value == "backcolor") {
                        //alert(info.button.color);
                        _oFontToolBar.getButtonByIndex("6").set("value", "#"+info.button.color);
                    }
            }

        });
    }

    this.showFontDialog = function() {

        //inicializa as propriedades de fonte do toolbar, referentes ao elemento selecionado
        _oClassOwner.setFontPropertiesOfToolBar();

        //habilita os botoes de acordo com o tipo de elemento selecionado
        _oFontToolBar.disableAllButtons();
        var oLayoutComposer = ModelInstance.getLayoutComposer();
        var oElement = oLayoutComposer.getSelectedElement();
        if(oElement != null) {
            switch(oElement.type) {
                case EElementType.eTLabel:
                    _oFontToolBar.enableButton(3);
                    _oFontToolBar.enableButton(4);
                    _oFontToolBar.enableButton(5);
                    break;
            }
        }

        _oFontDialog.render();
        _oFontDialog.show();
    }

    this.setFontPropertiesOfToolBar = function() {

        var oLayoutComposer = ModelInstance.getLayoutComposer();
        var oElement = oLayoutComposer.getSelectedElement();

        if(oElement != null) {
            switch(oElement.type) {
                case EElementType.eTLabel:
                    _oFontToolBar.getButtonByIndex("4").set("value", oElement.fontSize);
                    _oFontToolBar.getButtonByIndex("3").set("value", oElement.fontFamily);
                    _oFontToolBar.getButtonByIndex("5").set("value", oElement.fill);
                    break;
            }
        }
    }

    this.setFontPropertiesOfElementSelected = function() {

        var oLayoutComposer = ModelInstance.getLayoutComposer();
        var oElement = oLayoutComposer.getSelectedElement();

        if(oElement != null) {
            switch(oElement.type) {
                case EElementType.eTLabel:
                    oElement.fontSize = _oFontToolBar.getButtonByIndex("4").get("value");
                    oElement.fontFamily = _oFontToolBar.getButtonByIndex("3").get("value");
                    oElement.fill = _oFontToolBar.getButtonByIndex("5").get("value");
                    break;
            }
            oLayoutComposer.editPropertiesElement(oElement);
        }
    }

    this.getStrokeDashArrayValue = function(strName) {

        var strValue = EGeneralTypes.eStrokeDashSolidValue;
        
        switch(strName) {

            case EGeneralTypes.eStrokeDashSolidType:
                strValue = EGeneralTypes.eStrokeDashSolidValue;
                break;
            case EGeneralTypes.eStrokeDashTracedType:
                strValue = EGeneralTypes.eStrokeDashTracedValue;
                break;
        }
        return strValue;
    }

    this.getStrokeDashArrayName = function(strValue) {

        var strName = EGeneralTypes.eStrokeDashSolidType;

        switch(strValue) {

            case EGeneralTypes.eStrokeDashSolidValue:
                strName = EGeneralTypes.eStrokeDashSolidType;
                break;
            case EGeneralTypes.eStrokeDashTracedValue:
                strName = EGeneralTypes.eStrokeDashTracedType;
                break;
        }
        return strName;
    }

    /**
     * Chamada do inicializador da classe
     */
    this.init();
}
